#!/usr/bin/env bash

# ===================== Basic variable settings =====================

if test -e /usr/local/bin/f8x
then
    sleep 0.001
    # f8x -update
else
    curl -o f8x https://f8x.io/ && mv --force f8x /usr/local/bin/f8x && chmod +x /usr/local/bin/f8x && echo -e "\033[1;36m$(date +"%H:%M:%S")\033[0m \033[1;32m[INFOR]\033[0m - \033[1;32mInstalled f8x tools\033[0m" || echo -e "\033[1;36m$(date +"%H:%M:%S")\033[0m \033[1;31m[ERROR]\033[0m - \033[1;31mf8x installation failed\n\033[0m"
fi

. /usr/local/bin/f8x

F8x_dev_Version="0.0.2 Dev"

# ===================== Software version variable setting =====================

tomcat6_bin="apache-tomcat-6.0.53.tar.gz"
tomcat6_dir="apache-tomcat-6.0.53"
tomcat7_bin="apache-tomcat-7.0.109.tar.gz"
tomcat7_dir="apache-tomcat-7.0.109"
tomcat8_bin="apache-tomcat-8.5.68.tar.gz"
tomcat8_dir="apache-tomcat-8.5.68"
tomcat9_bin="apache-tomcat-9.0.50.tar.gz"
tomcat9_dir="apache-tomcat-9.0.50"

redis3_bin="redis-3.2.0.tar.gz"
redis3_dir="redis-3.2.0"
redis4_bin="redis-4.0.11.tar.gz"
redis4_dir="redis-4.0.11"
redis5_bin="redis-5.0.12.tar.gz"
redis5_dir="redis-5.0.12"

sharry_Ver="v1.9.0"
sharry_File="sharry-restserver-1.9.0.zip"

memcached160_bin="memcached-1.6.0.tar.gz"
memcached160_dir="memcached-1.6.0"

Main
Pentest_Base_Install > /dev/null 2>&1

# ===================== Install sharry =====================
sharry_Install(){

    name="sharry"

    if test -d $T_Dir/$name
    then
        Echo_ALERT "$name is already installed in $T_Dir/$name, run the following command to turn on the service:"
    else
        cd $T_Dir/ && rm -rf sharry-restserver-* && Echo_ALERT "Downloading $name (~70M)" && $Porxy_OK wget https://github.com/eikek/sharry/releases/download/$sharry_Ver/$sharry_File > /dev/null 2>&1 || Echo_ERROR2
        unzip $sharry_File > /dev/null 2>&1 && rm -f $sharry_File > /dev/null 2>&1 && mv --force sharry-restserver-* sharry && Echo_INFOR "Successfully installed $name, run the following command to turn on the service:" || Echo_ERROR3
    fi

    Echo_INFOR "$T_Dir/sharry/bin/sharry-restserver , the service requires a jdk environment, if not installed you can run the -oraclejdk option to install it."

}

# ===================== Install mariadb =====================
mariadb_Install(){

    name="mariadb"
    which mysql > /dev/null 2>&1

    if [ $? == 0 ]
    then
        Echo_ALERT "$name installed"
        Echo_INFOR "systemctl start mariadb"
    else
        Rm_Lock
        case $Linux_Version in
            *"CentOS"*|*"RedHat"*|*"Fedora"*)
                Install_Switch "mariadb"
                Install_Switch "mariadb-server"
                ;;
            *"Kali"*|*"Ubuntu"*|*"Debian"*)
                Install_Switch "mariadb-client"
                Install_Switch "mariadb-server"
                ;;
            *) ;;
        esac

        systemctl start mariadb
        mysql_secure_installation

        mysqlcmd="select User, host from mysql.user;
        GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'toor' WITH GRANT OPTION;"

        mysql -u root -p -e "${mysqlcmd}"
        Echo_INFOR "Default root/toor For security reasons, please do not deploy in a public network environment."
    fi

}

# ===================== Install mongodb =====================
mongodb_Install(){

    docker search mongodb   # 从Docker Hub查找镜像
    docker pull mongo       # 从镜像仓库中拉取或者更新指定镜像
    docker images mongo     # 列出本地主机上的mongo镜像
    docker run -d -p 27017:27017 --name mongodb mongo  # 创建一个新的容器并运行一个命令
    docker ps  # 显示正在运行的容器

    Echo_ALERT "Already listening to 0.0.0.0:27017, please do not deploy in public network environment"

}

# ===================== Install memcached =====================
memcached_Install(){

    Rm_Lock
    Install_Switch "memcached"

}

# ===================== Install memcached 1.6.0 =====================
memcached160_Install(){

    mkdir -p /tmp/memcached160 && cd /tmp/memcached160
    ls -al /usr/local/libevent-2.1.12-stable/lib | grep libevent > /dev/null 2>&1
    if [ $? == 0 ]
    then
        Echo_INFOR "libevent 2.1.12 installed"
    else
        $Porxy_OK wget https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz > /dev/null 2>&1 || Echo_ERROR2
        tar -zxvf libevent-2.1.12-stable.tar.gz > /dev/null 2>&1
        cd libevent-2.1.12-stable
        ./configure --prefix=/usr/local/libevent-2.1.12-stable
        make && make install && Echo_INFOR "Successfully installed libevent 2.1.12"
    fi
    cd /tmp/memcached160 && rm -rf $memcached160_bin && $Porxy_OK wget https://www.memcached.org/files/$memcached160_bin > /dev/null 2>&1 || Echo_ERROR2
    tar -zxvf $memcached160_bin > /dev/null 2>&1
    cd $memcached160_dir && ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent-2.1.12-stable/
    make && make test
    make install && Echo_INFOR "Successfully installed memcached 1.6.0"
    rm -rf /tmp/memcached160
    # 写了一大堆编译还报错,干脆直接软件包安装得了 :)

}

# ===================== Install postgresql =====================
postgresql_Install(){

#    Rm_Lock
#
#    case $Linux_Version in
#        *"CentOS"*|*"RedHat"*|*"Fedora"*)
#            Install_Switch "postgresql-server"
#            ;;
#        *"Kali"*|*"Ubuntu"*|*"Debian"*)
#            Install_Switch "postgresql"
#            Install_Switch "postgresql-contrib"
#            Install_Switch "sudo"
#            ;;
#        *) ;;
#    esac
#
#    sudo -u postgres psql -c "SELECT version();" 2>> /tmp/f8x_error.log

    Echo_INFOR "To allow external access, please refer to the following steps"
    Echo_INFOR "1. Configure to listen on all ports, modify \033[1;33m/etc/postgresql/11/main/postgresql.conf\033[0m \033[1;32madd\033[0m \033[1;33mlisten_addresses = '*'\033[0m"
    Echo_INFOR "2. Configure to allow external connections, modify \033[1;33m/etc/postgresql/11/main/pg_hba.conf\033[0m \033[1;32madd\033[0m \033[1;33mhost    all all 0.0.0.0/0   md5\033[0m"
    Echo_INFOR "3. Open firewall ports and restart services : \033[1;33mservice postgresql restart\033[0m"

    echo -e ""
    Echo_INFOR "Default is postgres user, no password, if external connection need to set password"
    Echo_INFOR "sudo -u postgres psql postgres"
    Echo_INFOR "\password postgres"
    Echo_INFOR "\q"

}

# ===================== Install redis3 =====================
redis3_Install(){

    Rm_Lock
    Install_Switch "tcl"

    case $Linux_Version in
        *"CentOS"*|*"RedHat"*|*"Fedora"*)
            Install_Switch "tcl-devel"
            ;;
        *"Kali"*|*"Ubuntu"*|*"Debian"*)
            Install_Switch "tcl-dev"
            ;;
        *) ;;
    esac

    name="redis3"

    cd /tmp && rm -rf $redis3_bin && $Porxy_OK wget http://download.redis.io/releases/$redis3_bin > /dev/null 2>&1 || Echo_ERROR2
    tar -zxf $redis3_bin > /dev/null 2>&1
    cd $redis3_dir
    make test
    make MALLOC=libc
    make install
    cd /tmp && rm -rf $redis3_bin

    mv --force /tmp/$redis3_dir/redis.conf /etc/redis.conf
    Echo_INFOR "redis.conf copied to /etc/redis.conf"
    rm -rf $redis3_dir

}

# ===================== Install redis4 =====================
redis4_Install(){

    Install_Switch "tcl"

    case $Linux_Version in
        *"CentOS"*|*"RedHat"*|*"Fedora"*)
            Install_Switch "tcl-devel"
            ;;
        *"Kali"*|*"Ubuntu"*|*"Debian"*)
            Install_Switch "tcl-dev"
            ;;
        *) ;;
    esac

    name="redis4"

    cd /tmp && rm -rf $redis4_bin && $Porxy_OK wget http://download.redis.io/releases/$redis4_bin > /dev/null 2>&1 || Echo_ERROR2
    tar -zxf $redis4_bin > /dev/null 2>&1
    cd $redis4_dir
    make test
    make MALLOC=libc
    make install
    cd /tmp && rm -rf $redis4_bin

    mv --force /tmp/$redis4_dir/redis.conf /etc/redis.conf
    Echo_INFOR "redis.conf copied to /etc/redis.conf"
    rm -rf $redis4_dir

}

# ===================== Install redis5 =====================
redis5_Install(){

    Rm_Lock
    Install_Switch "tcl"

    case $Linux_Version in
        *"CentOS"*|*"RedHat"*|*"Fedora"*)
            Install_Switch "tcl-devel"
            ;;
        *"Kali"*|*"Ubuntu"*|*"Debian"*)
            Install_Switch "tcl-dev"
            ;;
        *) ;;
    esac

    name="redis5"

    cd /tmp && rm -rf $redis5_bin && $Porxy_OK wget http://download.redis.io/releases/$redis5_bin > /dev/null 2>&1 || Echo_ERROR2
    tar -zxf $redis5_bin > /dev/null 2>&1
    cd $redis5_dir
    make test
    make MALLOC=libc
    make install
    cd /tmp && rm -rf $redis5_bin

    mv --force /tmp/$redis5_dir/redis.conf /etc/redis.conf
    Echo_INFOR "redis.conf copied to /etc/redis.conf"
    rm -rf $redis5_dir

}

# ===================== Install tomcat6 =====================

tomcat6_Install(){

    # Tomcat 6.0 requires JRE 5.0 or later.

    name="tomcat6"
    dir="/usr/local/$tomcat6_dir"

    if test -d $dir
    then
        Echo_ALERT "$name is already installed in $dir"
    else
        cd $T_Dir && rm -rf $tomcat6_bin && $Porxy_OK wget https://archive.apache.org/dist/tomcat/tomcat-6/v6.0.53/bin/$tomcat6_bin > /dev/null 2>&1 || Echo_ERROR2
        tar -zxf $tomcat6_bin -C /usr/local/ > /dev/null 2>&1 && rm -rf $tomcat6_bin

    fi

    rm -rf /usr/local/tomcat
    ln -s /usr/local/${tomcat6_dir}/ /usr/local/tomcat
    Echo_INFOR "Manually run the following command:\nexport CATALINA_HOME=/usr/local/tomcat\nexport PATH=\$CATALINA_HOME/bin:\$PATH"
    Echo_INFOR "catalina.sh version"
    Echo_INFOR "catalina.sh start"

}

# ===================== Install tomcat7 =====================

tomcat7_Install(){

    # Tomcat 7.0 requires Java 6 or later.

    name="tomcat7"
    dir="/usr/local/$tomcat7_dir"

    if test -d $dir
    then
        Echo_ALERT "$name is already installed in $dir"
    else
        cd $T_Dir && rm -rf $tomcat7_bin && $Porxy_OK wget https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.109/bin/$tomcat7_bin > /dev/null 2>&1 || Echo_ERROR2
        tar -zxf $tomcat7_bin -C /usr/local/ > /dev/null 2>&1 && rm -rf $tomcat7_bin
    fi

    rm -rf /usr/local/tomcat
    ln -s /usr/local/${tomcat7_dir}/ /usr/local/tomcat
    Echo_INFOR "Manually run the following command:\nexport CATALINA_HOME=/usr/local/tomcat\nexport PATH=\$CATALINA_HOME/bin:\$PATH"
    Echo_INFOR "catalina.sh version"
    Echo_INFOR "catalina.sh start"

}

# ===================== Install tomcat8 =====================

tomcat8_Install(){

    # Tomcat 8.5 requires Java 7 or later.

    name="tomcat8"
    dir="/usr/local/$tomcat8_dir"

    if test -d $dir
    then
        Echo_ALERT "$name is already installed in $dir"
    else
        cd $T_Dir && rm -rf $tomcat8_bin && $Porxy_OK wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.68/bin/$tomcat8_bin > /dev/null 2>&1 || Echo_ERROR2
        tar -zxf $tomcat8_bin -C /usr/local/ > /dev/null 2>&1 && rm -rf $tomcat8_bin

    fi

    rm -rf /usr/local/tomcat
    ln -s /usr/local/${tomcat8_dir}/ /usr/local/tomcat
    Echo_INFOR "Manually run the following command:\nexport CATALINA_HOME=/usr/local/tomcat\nexport PATH=\$CATALINA_HOME/bin:\$PATH"
    Echo_INFOR "catalina.sh version"
    Echo_INFOR "catalina.sh start"

}

# ===================== Install tomcat9 =====================

tomcat9_Install(){

    # Tomcat 9.0 requires Java 8 or later.

    name="tomcat9"
    dir="/usr/local/$tomcat9_dir"

    if test -d $dir
    then
        Echo_ALERT "$name is already installed in $dir"
    else
        cd $T_Dir && rm -rf $tomcat9_bin && $Porxy_OK wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.50/bin/$tomcat9_bin > /dev/null 2>&1 || Echo_ERROR2
        tar -zxf $tomcat9_bin -C /usr/local/ > /dev/null 2>&1 && rm -rf $tomcat9_bin

    fi

    rm -rf /usr/local/tomcat
    ln -s /usr/local/${tomcat9_dir}/ /usr/local/tomcat
    Echo_INFOR "Manually run the following command:\nexport CATALINA_HOME=/usr/local/tomcat\nexport PATH=\$CATALINA_HOME/bin:\$PATH"
    Echo_INFOR "catalina.sh version"
    Echo_INFOR "catalina.sh start"

}

# ===================== Install nginx (pkg) =====================
nginx_Install_pkg(){

    name="nginx"

    Rm_Lock
    Install_Switch "nginx"

    Echo_INFOR "systemctl status nginx"
    systemctl status nginx

}

# ===================== Install apache (pkg) =====================
apache_Install_pkg(){

    name="apache2"

    Rm_Lock
    case $Linux_Version in
        *"CentOS"*|*"RedHat"*|*"Fedora"*|*"AlmaLinux"*|*"VzLinux"*|*"Rocky"*)
            Install_Switch "httpd"
            Echo_INFOR "systemctl status httpd"
            systemctl status httpd
            ;;
        *"Kali"*|*"Ubuntu"*|*"Debian"*)
            Install_Switch "apache2"
            Echo_INFOR "systemctl status apache2"
            systemctl status apache2
            ;;
        *)
            ;;
    esac

}

# -help
Help_Info(){

    echo -e "\033[1;34mapache \033[0m"
    echo -e "  \033[1;34m-apache-pkg\033[0m \033[0;34m  : install apache (use apt/yum) \033[0m"
    echo -e ""
    echo -e "\033[1;34mnginx \033[0m"
    echo -e "  \033[1;34m-nginx-pkg\033[0m \033[0;34m   : install nginx (use apt/yum) \033[0m"
    echo -e "  \033[1;34m-nginx-src\033[0m \033[0;34m   : install nginx (use Source Code) \033[0m"
    echo -e ""
    echo -e "\033[1;34mtomcat \033[0m"
    echo -e "  \033[1;34m-tomcat6\033[0m \033[0;34m     : install tomcat6 \033[0m"
    echo -e "  \033[1;34m-tomcat7\033[0m \033[0;34m     : install tomcat7 \033[0m"
    echo -e "  \033[1;34m-tomcat8\033[0m \033[0;34m     : install tomcat8 \033[0m"
    echo -e "  \033[1;34m-tomcat9\033[0m \033[0;34m     : install tomcat9 \033[0m"
    echo -e ""
    echo -e "\033[1;34mDatabase \033[0m"
    echo -e "  \033[1;34m-mariadb\033[0m \033[0;34m     : install mariadb   (Package Manager) \033[0m"
    echo -e "  \033[1;34m-memcached\033[0m \033[0;34m   : install memcached (Package Manager) \033[0m"
    echo -e "  \033[1;34m-mongodb\033[0m \033[0;34m     : install mongodb   (docker) \033[0m"
    echo -e "  \033[1;34m-postgresql\033[0m \033[0;34m  : install postgresql(Package Manager) \033[0m"
    echo -e "  \033[1;34m-redis3\033[0m \033[0;34m      : install redis3 (Compile) \033[0m"
    echo -e "  \033[1;34m-redis4\033[0m \033[0;34m      : install redis4 (Compile) \033[0m"
    echo -e "  \033[1;34m-redis5\033[0m \033[0;34m      : install redis5 (Compile) \033[0m"
    echo -e ""
    echo -e "\033[1;34mOther \033[0m"
    echo -e "  \033[1;34m-sharry\033[0m \033[0;34m      : install sharry\033[0m"

}

for cmd in $@
do
    case $cmd in
        -sharry)
            Porxy_Switch
            Base_Check
            sharry_Install
            ;;
        -mariadb)
            Porxy_Switch
            Base_Check
            mariadb_Install
            ;;
        -mongodb)
            Base_Check
            Docker_Check
            mongodb_Install
            ;;
        -memcached)
            Porxy_Switch
            Base_Check
            memcached_Install
            ;;
        -postgresql)
            Porxy_Switch
            Base_Check
            postgresql_Install
            ;;
        -redis3)
            Porxy_Switch
            Base_Check
            redis3_Install
            ;;
        -redis4)
            Porxy_Switch
            Base_Check
            redis4_Install
            ;;
        -redis5)
            Porxy_Switch
            Base_Check
            redis5_Install
            ;;
        -tomcat6)
            Porxy_Switch
            Base_Check
            tomcat6_Install
            ;;
        -tomcat7)
            Porxy_Switch
            Base_Check
            tomcat7_Install
            ;;
        -tomcat8)
            Porxy_Switch
            Base_Check
            tomcat8_Install
            ;;
        -tomcat9)
            Porxy_Switch
            Base_Check
            tomcat9_Install
            ;;
        -nginx-src)
            nginx_Install
            ;;
        -nginx-pkg)
            nginx_Install_pkg
            ;;
        -apache-pkg)
            apache_Install_pkg
            ;;
        -help | help)
            printf "\033c"
            Help_Info
            exit 1
            ;;
    esac
done

echo -e "\033[1;36m \n-----OVER-----\n \033[0m"
